package org.jeecg.common.nc.utils;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import net.sf.json.JSONArray;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JSON转换工具
 */
public class JSONUtils {
    private static Gson g = new Gson();
    private static ObjectMapper mapper = new ObjectMapper();




    public static ObjectMapper getMapper() {
        return mapper;
    }

    /**
     * JSON转换工具
     * @param target
     * @return
     */
    public static String toJsonFromObject(Object target) {
        JSONArray jsonArray = new JSONArray();
        jsonArray = JSONArray.fromObject(target);
        String str=jsonArray.get(0).toString();
        str=str.replaceAll("null","\"\"").replaceAll("NULL","\"\"");
        return str;
    }
    /**
     * JSON转换工具
     * @param target
     * @return
     */
    public static JSONArray toJSONArrayFromObject(Object target) {
        JSONArray jsonArray = new JSONArray();
        jsonArray = JSONArray.fromObject(target);
        return jsonArray;
    }
    public static String writeValueAsString(Object e)
            throws JsonProcessingException {
        return mapper.writeValueAsString(e);
    }

    public static String toJSON(Object obj) {
        try {
            String json = mapper.writeValueAsString(obj);
            return json;
        } catch (JsonProcessingException e) {
            return e.getMessage();
        }
    }

    public static <T> T toObject(String json, Class<T> cls) {
        try {
            return mapper.readValue(json, cls);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @author shixtf
     * @description  String 转集合
     * @date  2019/4/16  19:35
     **/

    public static <T> List<T> toList(String json, Class<T> cls) {
        try {
            JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class,cls);
            return mapper.readValue(json, javaType);
        } catch (Exception e) {
            return null;
        }
    }

    public static String toJSON( Map<String, String> map) {
        return g.toJson(map);
    }
    public static Gson getGson() {
        return g;
    }



    public static String toJSON(HashMap  map1, HashMap  map2) {
        String s = g.toJson(map1);
        HashMap smap = g.fromJson(s, HashMap.class);
        smap.putAll(map2);
        return g.toJson(smap);
    }
    public static <T> T toBean(String json,Class<T> t) {
        T temp = g.fromJson(json, t);
        return temp;
    }

    public static String toJSON(List list) {
        return g.toJson(list);
    }

    public static String toJSON(String json1, String json2) {
        HashMap smap = g.fromJson(json1, HashMap.class);
        HashMap smap2 = g.fromJson(json2, HashMap.class);
        smap.putAll(smap2);
        return g.toJson(smap);
    }

    public static String toMapJSON( Map<String, Object> map) {
        return g.toJson(map);
    }
}
